﻿using System;
using System.Threading.Tasks;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            var host = new WebHostBuilder()
               //.UseConfiguration(config)
               //.UseKestrel()
               .UseTestServer()
               .Configure(app =>
               {
                   app.UseMiddleware<LogMiddleWare>();
                   app.Use(async (context, next) => {
                       await Console.Out.WriteLineAsync(context.Url + "Call before next.");
                       await next(context);
                       await Console.Out.WriteLineAsync(context.Url + "Call after next.");
                   });
                   app.Run(async (context) => {
                       await Console.Out.WriteLineAsync("hello world!");
                   });
               })
               .Build();

            host.Run();
            Console.ReadLine();
        }
    }

    public class LogMiddleWare
    {
        private readonly RequestDelegate _next;
        public LogMiddleWare(RequestDelegate next)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            _next = next;
        }
        
        public async Task Invoke(RequestContext context){
            await Console.Out.WriteLineAsync("before log!");
            await _next(context);
            await Console.Out.WriteLineAsync("after log!");
        }
    }
}
